package com.example.home.me;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.example.home.R;
import com.example.home.bean.MeBean;
import com.example.home.bean.PersonInfoUser;
import com.example.home.tool.OKHttpTool;
import com.google.gson.Gson;

import org.json.JSONException;
import org.json.JSONObject;

public class MeInfoActivity extends AppCompatActivity {

    private EditText edt_name;
    private EditText edt_sex;
    private EditText edt_iph;
    private Button save;
    private MeBean meBean;
    private MeBean meBean1;
    private Handler handler;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_me_info);
        initView();
        setDatatoPaeg();
    }

    private void initView() {
        edt_name = findViewById(R.id.edt_name);
        edt_sex = findViewById(R.id.edt_sex);
        edt_iph = findViewById(R.id.edt_iph);
        save = findViewById(R.id.save);
        handler = new Handler();

        Intent intent = getIntent();
        String usePersonJson = intent.getStringExtra("name");
        Gson gson = new Gson();
        meBean = gson.fromJson(usePersonJson, MeBean.class);
        PersonInfoUser.USERNAME = meBean.getUser().getNickName();
        PersonInfoUser.SEX = meBean.getUser().getSex();
        PersonInfoUser.PHONE = meBean.getUser().getPhonenumber();


        save.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                binviewlistbean();

            }
        });

        ImageView back = findViewById(R.id.back);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        
    }

    private void setDatatoPaeg() {
        edt_name.setText(PersonInfoUser.USERNAME);
        if (PersonInfoUser.SEX.equals("0") || PersonInfoUser.SEX.equals("男")){
            edt_sex.setText("男");
        }else if (PersonInfoUser.SEX.equals("1") || PersonInfoUser.SEX.equals("女")){
            edt_sex.setText("女");
        }
        if (PersonInfoUser.PHONE.length() >= 4) {
            int length = meBean.getUser().getPhonenumber().length();
            String maskedPhoneNumber = PersonInfoUser.PHONE.substring(0, length - 4) + "****"; // 使用 "*" 代替后四位
            edt_iph.setText(maskedPhoneNumber);
        } else {
            edt_iph.setText(PersonInfoUser.PHONE);
        }
    }


    private void binviewlistbean() {
        PersonInfoUser.USERNAME = edt_name.getText().toString();
        PersonInfoUser.SEX = edt_sex.getText().toString();
        PersonInfoUser.PHONE = edt_iph.getText().toString();
        Log.e("TAG1",edt_name.getText().toString());
        String userName = edt_name.getText().toString();
        if(TextUtils.isEmpty(userName)){
            Toast.makeText(this, "", Toast.LENGTH_SHORT).show();
            return;
        }

        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("nickName", PersonInfoUser.USERNAME);
            jsonObject.put("sex", PersonInfoUser.SEX);
            jsonObject.put("phonenumber", PersonInfoUser.PHONE);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        String json = jsonObject.toString();

        doNet(json);
    }

    private void doNet(String json1) {
        OKHttpTool.doNetRequest("prod-api/api/common/user",
                OKHttpTool.PUT,
                MeBean.class,
                new OKHttpTool.NetCallback() {
                    @Override
                    public void onSuccess(Object obj) {
                        meBean1 = (MeBean) obj;
                        if(meBean1 .getCode() ==200){
                            handler.post(new Runnable() {
                                @Override
                                public void run() {
                                    Toast.makeText(getApplicationContext(),"修改成功",Toast.LENGTH_SHORT).show();
                                    finish();
                                }
                            });
                        }
                    }

                    @Override
                    public void onFailure(String msg) {

                    }
                },json1
        );
    }

}